﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Simnova.Collaborate.Presenter;
using Simnova.Collaborate.View;
using System.Collections;

namespace Simnova.Collaborate.WinForm
{
    public partial class ReportViewer : Form, IReportViewer
    {
        private int userId;
        private ReportViewerPresenter reportViewerPresenter;

        public ReportViewer(int userId)
        {
            InitializeComponent();
            this.userId = userId;
        }

        public void ExecuteWeeklyReport()
        {
            
        }


        #region IReportViewer Members

        DateTime IReportViewer.WeeklyReportEndDate
        {
            get {
                return System.DateTime.Parse(weeklyReportcomboBox.SelectedValue.ToString()); 
            }
        }

        DateTime IReportViewer.DailyReportDate
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        private void ReportViewer_Load(object sender, EventArgs e)
        {
            reportViewerPresenter = new ReportViewerPresenter(this, this.userId);
            populateMondays();
            

            
        }

        private void populateMondays()
        {
            
            //System.Collections.Specialized.NameValueCollection mondayList = new System.Collections.Specialized.NameValueCollection();

            DateTime mostRecentMonday;
            int dayOfWeek = (int)DateTime.Now.DayOfWeek;
            if (dayOfWeek == 0)
            {
                //it is sunday
                mostRecentMonday = DateTime.Now.AddDays(-6);
            }
            else
            {
                mostRecentMonday = DateTime.Now.AddDays(1 - dayOfWeek);
            }
            IList mondayList = new ArrayList();
            mondayList.Add(new 
                {
                    Name = mostRecentMonday.ToShortDateString(),
                    Value = mostRecentMonday
                });

            for (int i = 0; i < 51; i++)
            {
                mostRecentMonday = mostRecentMonday.AddDays(-7);
                mondayList.Add(new 
                    {
                        Name = mostRecentMonday.ToShortDateString(),
                        Value = mostRecentMonday
                    });
            }
            weeklyReportcomboBox.DataSource = mondayList;

            this.weeklyReportcomboBox.DisplayMember = "Name";
            this.weeklyReportcomboBox.ValueMember = "Value";

        }

        private void executeWeeklyReportButton_Click(object sender, EventArgs e)
        {
            byte[] report = reportViewerPresenter.ViewWeeklyReport();
            string fileName = (new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location)).DirectoryName.ToString() + @"\weeklyReport_" + DateTime.Now.ToShortDateString().Replace("/", "-") + "_" + DateTime.Now.ToShortTimeString().Replace(":","-") + ".pdf";
            
            System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate);
            fs.Write(report, 0, report.Length);
            fs.Close();
            
            System.Diagnostics.Process.Start(fileName);
        }
    }
}
